package com.timoon.webcomponent.utility.product.single.component;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.sample.dao.model.Product;
import com.timoon.sample.dao.model.ProductInstructionPic;
import com.timoon.sample.dao.model.ProductProperty;

/**
 *<p>商品属性及详情展示展示,用于选项卡</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class ProductDetailTabPanel extends Panel {

	@SpringBean(name="com.timoon.webcomponent.utility.product.service.ProductService")
	private ProductDetailTabService service;
	
	private Integer productId;
	
	private Product product;
	
	private int cols=2;//列数
	
	
	public ProductDetailTabPanel(String id,Integer productId) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.productId=productId;
		
		renderPanel();
	}
	public ProductDetailTabPanel(String id,Integer productId,int cols) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.productId=productId;
		
		this.cols=cols;
		
		renderPanel();
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		this.product=this.service.getProductById(productId);
		
		renderDetail();
		
		renderInstruction();
		
		renderInstructionPic();
	}

	private void renderInstructionPic() {
		// TODO Auto-generated method stub
		List<ProductInstructionPic> instructionPics=this.service.getProductInstructionPicsByProductId(productId);
		
		ListView<ProductInstructionPic> listView=new ListView<ProductInstructionPic>("wk_pic_list",instructionPics) {
			
			@Override
			protected void populateItem(ListItem<ProductInstructionPic> arg0) {
				// TODO Auto-generated method stub
				arg0.add(new Image("wk_pic",new PackageResourceReference(this.getClass(),"name.jpg")));
			}
		};
		
		add(listView);
	}
	private void renderInstruction() {
		// TODO Auto-generated method stub
		
		add(new Label("wk_instruction",this.product.getInstruction()));
	}
	private void renderDetail() {
		// TODO Auto-generated method stub
		
		List<ProductProperty> list=this.service.getProductPropertyListByProductId(productId);
		
		IDataProvider<ProductProperty> data=new ListDataProvider<>(list);
		GridView<ProductProperty> grid=new GridView<ProductProperty>("rows",data){	

		@Override
		protected void populateEmptyItem(Item<ProductProperty> arg0) {
			// TODO Auto-generated method stub
			arg0.add(new Label("wk_property",""));
		}

		@Override
		protected void populateItem(Item<ProductProperty> arg0) {
			// TODO Auto-generated method stub
			ProductProperty property=arg0.getModelObject();
			arg0.add(new Label("wk_property",property.getName()+":"+property.getValue()));
		}};
		
		if(list!=null&&list.size()>0){
			int total=list.size();
			int rows=total/this.cols;
			if(total%this.cols!=0){
				rows++;
			}
			grid.setRows(rows);
			grid.setColumns(this.cols);
		}else{
			grid.setRows(1);
			grid.setColumns(1);
		}
		add(grid);
	}

	public ProductDetailTabService getService() {
		return service;
	}

	public void setService(ProductDetailTabService service) {
		this.service = service;
	}

}
